﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    //单例模式：指一个系统中只存在一个实例，同时提供集中统一的接口，以使系统行为保持一直。
    //singleton一此在逻辑学种指“有且仅有一个元素的集合”，这非常恰当地概括了单例的概念
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }

    //饿汉方式
    public class Sun1
    {
        private static Sun1 instance=new Sun1();
        private Sun1()
        {

        }

        public static Sun1 GetInstance()
        {
            return instance;
        }
    }

    //懒汉方式
    public class Sun2
    {
        private static Sun2 instance;
        private static object ob=new object();

        private Sun2()
        {

        }

        public static Sun2 GetInstance()
        {
            if(instance==null)
            {
                lock(ob)
                {
                    if(instance==null)
                    {
                           instance = new Sun2();
                    }
                }
             
            }
            return instance;
        }
    }
     
}
